package com.lqk.client;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

public class ClientThread implements Runnable{

    private Selector selector;

    public ClientThread(Selector selector) {
        this.selector = selector;
    }

    @Override
    public void run() {
        try{
            for (;;){
                // 获取channel数量
                int readChannels = selector.select();

                if (readChannels==0){
                    continue;
                }
                // 获取可用的channel
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                // 遍历
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()){
                    SelectionKey selectionKey = iterator.next();
                    //移除set集合中当前selectionKey
                    iterator.remove();
                    // 6 如果是可读状态
                    if (selectionKey.isReadable()){
                        readOperator(selector,selectionKey);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 处理可读状态操作
    private void readOperator(Selector selector, SelectionKey selectionKey) throws Exception {
        // 1 从SelectionKey获取到已经就绪的通道
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
        // 2 创建Buffer
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        // 3 循环读取客户端消息
        int readLength = socketChannel.read(byteBuffer);
        String message="";
        if (readLength>0){
            // 切换为读模式
            byteBuffer.flip();
            // 读取内容
            message += Charset.forName("UTF-8").decode(byteBuffer);
        }
        // 4 将channel再次注册到选择器上，监听可读状态
        socketChannel.register(selector,SelectionKey.OP_READ);
        // 5 接收消息并打印
        if (message.length()>0){
            System.out.println(message);
        }
    }
}
